/// <summary>
// CodeFx - A .NET framework
// Laurent Morisseau
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Drawing;
using System.IO;

namespace CodeFx.Web
{
    public static class UrlHelper
    {
        public static string CreateThumb(string userdir, string filenm, string thumbsdir, int FinalOutWidth, int FinalOutHeight, bool thumb)
        {
            string t_thumbfn = String.Concat(NicePath(thumbsdir), "\\", filenm);
            string _return = "";
            string chemin = String.Concat(NicePath(userdir), "\\", filenm);
            if (File.Exists(chemin))
            {
                string _path = NicePath(thumbsdir);
                if (!Directory.Exists(_path))
                    Directory.CreateDirectory(_path);


                Bitmap graphic = new Bitmap(chemin);
                int thumbWidth = graphic.Width;
                int thumbHeight = graphic.Height;

                FileHelper.Resize(ref thumbWidth, ref thumbHeight, FinalOutWidth, FinalOutHeight);

                if (thumb)
                {
                    Image imgThumbnail = graphic.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
                    imgThumbnail.Save(t_thumbfn, graphic.RawFormat);
                    if (graphic != null)
                        graphic.Dispose();

                }
                else
                {
                    Bitmap BmpPhotoMini = new Bitmap(thumbWidth, thumbHeight);
                    Graphics graphicsPhotoMini = Graphics.FromImage(BmpPhotoMini);
                    graphicsPhotoMini.DrawImage(graphic, new Rectangle(0, 0, thumbWidth, thumbHeight), 0, 0, graphic.Width, graphic.Height, System.Drawing.GraphicsUnit.Pixel);
                    System.Drawing.Imaging.ImageFormat gif = graphic.RawFormat;
                    graphic.Dispose();
                    graphicsPhotoMini.Dispose();

                    if (File.Exists(t_thumbfn))
                        File.Delete(t_thumbfn);

                    BmpPhotoMini.Save(t_thumbfn, gif);
                    BmpPhotoMini.Dispose();
                }
                _return = t_thumbfn.Replace("\\", "/");

            }
            return _return;
        }
        public static string NiceName(string _string)
        {
            string _return = _string;
            if (_return != null)
            {

                _return = _return.Replace("'", " ");
                _return = _return.Replace("\"", " ");
            }
            return _return;
        }

        public static string NicePath(string t_input)
        {
            if (string.IsNullOrEmpty(t_input))
                return string.Empty;
            string _NicePath = t_input.Replace("/", @"\");
            if (_NicePath.Length > 0)
            {
                _NicePath = _NicePath.Replace(@"\\\", @"\");
                _NicePath = _NicePath.Replace(@"\\", @"\");
                if (_NicePath.LastIndexOf(@"\") == _NicePath.Length - 1)
                    _NicePath = _NicePath.Remove(_NicePath.Length - 1);
                if (_NicePath.IndexOf(@"\") == 0)
                    _NicePath = _NicePath.Remove(0, 1);
            }
            return _NicePath;
        }

        public static string NiceFN(string t_input)
        {
            if (string.IsNullOrEmpty(t_input))
                return string.Empty;
            string _NicePath = t_input.Replace("_", "/");
            if (_NicePath.IndexOf(".") > 1)
                _NicePath = _NicePath.Remove(1, _NicePath.IndexOf(".") - 1);
            _NicePath = _NicePath.Replace("-", " ");
            _NicePath = _NicePath.Replace(".", " ");
            return _NicePath;
        }

        public static string NiceUrl(string url, string guid)
        {
            string et = AndOr(url);
            if (!string.IsNullOrEmpty(guid))
                return string.Concat(url,et,"GUID=", guid, "&");
            else
                return string.Concat (url,et);
        }

        public static string AndOr(string Page)
        {
            if (!string.IsNullOrEmpty (Page))
            {
                if (Page.Contains("?"))
                    return "&";
                else
                    return "?";
            }
            return "?";
        }


        public static string NiceUrl(string guid)
        {
            if (!string.IsNullOrEmpty(guid))
                return string.Concat("?GUID=", guid, "&");
            else
                return "?";
        }

        public static string NiceUrl()
        {
            string _return;
            if (HttpContext.Current.Request["GUID"] != null)
                _return = string.Concat("?GUID=", HttpContext.Current.Request["GUID"].ToString(), "&");
            else if (Cookie.IsNull(ConstantesBase.Persistance.User))
                return string.Concat("?GUID=", MyGuid.Current, "&");
            else
                _return = "?";
            return _return;
        }
        public static string NicePathWeb(string t_input)
        {
            string _NicePath = (string)null;
            if (!string.IsNullOrEmpty(t_input))
            {
                _NicePath = t_input.Replace("\\", "/");
                if (_NicePath.Length > 0)
                {
                    _NicePath = _NicePath.Replace("///", "/");
                    _NicePath = _NicePath.Replace("//", "/");
                    if (_NicePath.LastIndexOf("/") == _NicePath.Length - 1)
                        _NicePath = _NicePath.Remove(_NicePath.Length - 1);
                }
            }
            return _NicePath;
        }

        public static string SetGuid()
        {
            return string.Concat("?GUID=", MyGuid.Current, "&");
        }

        public static string NiceOverlib(string _string)
        {
            string _return = _string;
            if (!string.IsNullOrEmpty(_return))
            {
                if (_string.IndexOf(Environment.NewLine) > 0)
                    _return = _return.Replace(Environment.NewLine, "<br/>");

                _return = _return.Replace("'", "\\'");
                _return = _return.Replace("\"", "\\'");
            }
            return _return;
        }

        public static string MakeVirtualPathAppAbsolute(string virtualPath)
        {
            return MakeVirtualPathAppAbsolute(virtualPath, HttpRuntime.AppDomainAppVirtualPath);
        }

        private static string MakeVirtualPathAppAbsolute(string virtualPath, string applicationPath)
        {
            if ((virtualPath.Length == 1) && (virtualPath[0] == '~'))
            {
                return applicationPath;
            }
            if (((virtualPath.Length >= 2) && (virtualPath[0] == '~')) && ((virtualPath[1] == '/') || (virtualPath[1] == '\\')))
            {
                if (applicationPath.Length > 1)
                {
                    return (applicationPath + virtualPath.Substring(2));
                }
                return ("/" + virtualPath.Substring(2));
            }
            if (!IsRooted(virtualPath))
            {
                throw new ArgumentOutOfRangeException("virtualPath");
            }
            return virtualPath;
        }
        private static bool IsRooted(string basepath)
        {
            if (!string.IsNullOrEmpty(basepath) && (basepath[0] != '/'))
            {
                return (basepath[0] == '\\');
            }
            return true;
        }
    }
}
